package tacos.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import tacos.entity.Ingredient;
import tacos.data.springData.IngredientRepository;

import java.util.Optional;

@Component
public class IngredientConverter implements Converter<String, Ingredient> {

    private IngredientRepository ingredientRepository;

    @Autowired
    public  IngredientConverter(IngredientRepository ingredientRepository) {
        this.ingredientRepository = ingredientRepository;
    }

    @Override
    public Ingredient convert(String ingredientId) {

        Optional<Ingredient> byId = ingredientRepository.findById(ingredientId);
        return byId.isPresent() ? byId.get() : null;
    }
}
